package dao

import (
	"context"
	"errors"
	"sale-admin/internal/dal"
	"sale-admin/internal/models"
	"strings"
	"time"

	"192.168.1.75/go-pkg/errorx"
)

type RobotReportConfig struct{}

func (RobotReportConfig) Create(ctx context.Context, query *dal.Query, merchantID string, PutType, RobotSelectType int,
	MerchantSelect, RobotName string, ObjectName string, WebHookName int, WebHookAddress string) (err error) {
	m := query.MaRobotConfig
	var d models.MaRobotConfig
	d.Type = 1
	d.PutType = PutType // 推送类型:1运营日报 2单日空货 3连续空货 4电量报告 5空货周报
	d.Object = merchantID
	d.RobotSelectType = RobotSelectType
	d.MerchantSelect = MerchantSelect
	d.RobotName = RobotName
	d.AddTime = time.Now()
	d.ObjectName = ObjectName
	d.WebHookName = WebHookName
	d.WebHookAddress = WebHookAddress
	err = m.WithContext(ctx).Create(&d)
	if err != nil {
		return errorx.New("添加失败", -1)
	}
	return
}
func (RobotReportConfig) Update(ctx context.Context, query *dal.Query, merchantID string, ID string, PutType,
	RobotSelectType int, MerchantSelect, RobotName string, ObjectName string, WebHookName int, WebHookAddress string) (err error) {
	m := query.MaRobotConfig
	tx := m.WithContext(ctx).Where(m.ID.Eq(ID))
	if merchantID != "1" {
		tx = tx.Where(m.Object.Eq(merchantID))
	}
	res, err := tx.UpdateSimple(
		m.PutType.Value(PutType),
		m.RobotSelectType.Value(RobotSelectType),
		m.MerchantSelect.Value(MerchantSelect),
		m.RobotName.Value(RobotName),
		m.ObjectName.Value(ObjectName),
		m.WebHookName.Value(WebHookName),
		m.WebHookAddress.Value(WebHookAddress),
	)
	if res.RowsAffected < 0 || err != nil {
		return errors.New("更新失败")
	}
	return
}

func (RobotReportConfig) Detail(ctx context.Context, query *dal.Query, merchantID string, ID string, PutType int) (
	*models.MaRobotConfig, int64, error) {
	m := query.MaRobotConfig
	tx := m.WithContext(ctx)
	if merchantID != "" {
		tx = tx.Where(m.Object.Eq(merchantID))
	}
	if ID != "" {
		tx = tx.Where(m.ID.Eq(ID))
	}
	if PutType != 0 {
		tx = tx.Where(m.PutType.Eq(PutType))
	}
	info, _ := tx.Take()
	var count int64
	if info != nil && info.ID != "" {
		count = 1
	}
	return info, count, nil
}
func (RobotReportConfig) List(ctx context.Context, query *dal.Query, merchantID string, ID string, PutType, page, limit int) (
	[]*models.MaRobotConfig, int64, error) {
	m := query.MaRobotConfig
	tx := m.WithContext(ctx)
	if merchantID != "" {
		tx = tx.Where(m.Object.In(strings.Split(merchantID, ",")...))
	}
	if ID != "" {
		tx = tx.Where(m.ID.Eq(ID))
	}
	if PutType != 0 {
		tx = tx.Where(m.PutType.Eq(PutType))
	}
	offset, limit := Offset(page, limit)
	info, count, _ := tx.FindByPage(offset, limit)
	return info, count, nil
}

func (RobotReportConfig) Delete(ctx context.Context, query *dal.Query, ID string) (err error) {
	if ID == "" {
		return errorx.New("ID不能为空")
	}
	m := query.MaRobotConfig
	res, err := m.WithContext(ctx).
		Where(m.ID.Eq(ID)).
		Delete()
	if res.RowsAffected == 0 || err != nil {
		return errorx.New("删除失败")
	}
	return
}
